import json
from .metadata import *

#比如java语言的可以改成parse_java
def parse_sample(json_data):
        def extract_freq_info(obj:dict):
                return FileFrequencyFeatureInfo(
                    comment_type=obj["CommentTypeFrequency"],
                    word=obj["wordFrequency"],
                    word_number_of_line=obj["WordNumberOfLineFrequency"],
                    line_length=obj["LineLengthFrequency"],
                    keyword=obj["keywordFrequency"],
                    ast_leaves=obj["ASTLeavesFrequency"],
                    identifier_length=obj["IndentifierLengthFrequency"],
                    access_control=obj["AccessControlFrequency"]
                )
                
        def extract_scalar_info(obj:dict):
            return FileScalarFeatureInfo(
                new_usage_number=obj["NewUsageNumber"],
                old_usage_number=obj["OldUseageNumber"],
                safety_usage_number=obj["SafetyUsageNumber"],
                comment_number=obj["CommentNumber"],
                word_number=obj["WordNumber"],
                ternary_operator_number=obj["TernaryOperatorNumber"],
                control_struct_number=obj["ControlStructNumber"],
                literal_number=obj["LiteralNumber"],
                blank_line_number=obj["BlankLineNumberNumber"],
                tab_indent_number=obj["TabIndentNumber"],
                new_line_before_open_brance_number=obj["NewLineBeforeOpenBranceNumber"],
                on_line_before_open_brance_number=obj["OnLineBeforeOpenBranceNumber"]
            )
            
        def extract_file_info(obj:dict):
            file_features=FileFeatureInfo(
                extract_scalar_info(obj["CodeFeatures"])
                ,extract_freq_info(obj["CodeFeatures"]))
            
            return FileInfo(
                file_name=obj["FileName"],
                file_path=obj["FilePath"],
                file_length=obj["FileLength"],
                file_line_number=obj["FileLineNumber"],
                file_features=file_features
                )
            
        obj=json.loads(json_data)
        #print(type(obj))
        extract_handle=extract_file_info
        person_info=PersonInfo(obj["PersonName"],obj["PersonPath"])
        file_infos=[extract_handle(e) for e in obj["FileFeatures"]]




        return PersonSample(person_info,file_infos)

if __name__=="__main__":
    test_dir='data/java'
    parser_test(test_dir,parse_sample)